﻿<?php
//$OJ_CACHE_SHARE=true;
include("include/header.php");
require_once("./include/db_info.inc.php");
require_once("./include/my_func.inc.php");
require_once("./include/const.inc.php");
include_once("maincore.php");
?>
<div class="body-wrap nobg">
	<div class="wrapper">
		
		
		<div class="main">
			<div class="col-a1">
<?php
if (!defined("THNOJ")) die ("Outside of the site");
if (isset($_GET['cid'])){
	$cid=intval($_GET['cid']);
//	print $cid;
	$congkhai=true;
	//require_once("contest-header.php");
	// check contest valid
	$sql="SELECT * FROM `contest` WHERE `contest_id`='$cid' AND (author_id = 'admin'||private=0) and `defunct`='N' ";
	if ($DEBUG) echo $sql;
	$result=mysql_query($sql);
	$rows_cnt=mysql_num_rows($result);
	echo "<center>";
	if ($rows_cnt==0){
		mysql_free_result($result);
		echo "<h2>Không có kì thi như vậy!</h2>";
		//require_once("oj-footer.php");
		exit(0);
	}else{
		$row=mysql_fetch_object($result);
		$now=time();
		$start_time=strtotime($row->start_time);
		$end_time=strtotime($row->end_time);
		//echo "<title>Contest - $row->title</title>";
		echo "<h3>$row->title</h3>";
		/*
		echo "Kết thúc: <font color=#993399>$row->end_time</font><br>";
		echo "Giờ hiện tại: <font color=#993399><span id=nowdate >".date("Y-m-d H:i:s")."</span></font> Trạng thái: ";
		if ($now>$end_time) echo "<font color=red>Đã kết thúc</font>";
		else if ($now<$start_time) echo "<font color=red>Chưa bắt đầu</font>";
		else echo "<font color=red>Đang thi</font>";
		if ($row->private=='0') {
			echo "&nbsp;&nbsp;<font color=blue>Mở</font>";
		} else {
			echo "&nbsp;&nbsp;<font color=red>Kín</font>"; 
			$congkhai=false;
		}
		if (($_SESSION['user_rights']!="A") && $now<$start_time){
			echo "</center>";
			//require_once("oj-footer.php");
			exit(0);
		}
		*/
	}
	/*
	if (!$congkhai) {
		$sql="select * from privilege where user_id='".$_SESSION['user_id']."' and rightstr='c$cid' limit 1";
		$result=mysql_query($sql) or die(mysql_error());
		if (!mysql_num_rows($result)){
			echo "<br><h1>Không được tham gia!</h1>";
			//require_once("oj-footer.php");
			exit(1);
		}
	}
	*/
	$sql="SELECT `problem`.`title` as `title`,`problem`.`problem_id` as `pid`
		FROM `contest_problem`,`problem`
		WHERE `contest_problem`.`problem_id`=`problem`.`problem_id` AND `problem`.`defunct`='N'
		AND `contest_problem`.`contest_id`='$cid' ORDER BY `contest_problem`.`num`";
//	echo $cid;
//	echo "<br>";
//	echo $sql;
//	echo "<br>";
	$result=mysql_query($sql);
	$cnt=0;
	echo "<table width=60% class='contestlist'><tr class=toprow><td width=5></td><td width=34%><b>Mã đề bài</b></td><td width=65%><b>Tiêu đề</b></td></tr>";
	while ($row=mysql_fetch_object($result)){
		if ($cnt&1) echo "<tr class=oddrow>";
		else echo "<tr class=evenrow>";
		echo "<td>";
		if (isset($_SESSION['user_id'])) echo check_ac($cid,$cnt);
		echo "<td>$row->pid
			<td><a href='exercise.php?id=$row->pid'>$row->title</a>
			</tr>";
		$cnt++;
	}
	echo "</table><br>";
	mysql_free_result($result);
	//echo "[<a href='status.php?cid=$cid'>Status</a>]";
	//echo "[<a href='contestrank.php?cid=$cid'>Xếp hạng</a>]";
	//echo "[<a href='conteststatistics.php?cid=$cid'>Statistics</a>]";
	echo "</center>";
}else{

//require_once("oj-header.php");
?>
<!--<title>Danh sách kì thi</title>-->
<?php 
//echo $_SESSION['user_reference']."<br>";
$sql="SELECT * FROM `contest` WHERE `defunct`='N' and author_id='admin' ORDER BY `contest_id` DESC";
if ($DEBUG) echo $sql;
$result=mysql_query($sql);
$color=false;
echo "<center><h3>Chọn một mục sau: </h3><table width=90% class='contestlist'>";
//echo "<tr class=toprow align=center><td width='10%'>Mã</td><td width='50%'>Tiêu đề</td></tr>";
while ($row=mysql_fetch_object($result)){
	if ($color) echo "<tr align=center class=oddrow>";
	else echo "<tr align=center class=evenrow>";
	echo "<td>$row->contest_id</td>";
	echo "<td align='left'><a href='?cid=$row->contest_id'>$row->title</a></td>";
	//$start_time=strtotime($row->start_time);
	//$end_time=strtotime($row->end_time);
	//$now=time();
	// past
	//if ($now>$end_time) echo "<td><font color=green>Ended@$row->end_time</font>";
	//else if ($now<$start_time) echo "<td><font color=blue>Start@$row->start_time</font>";
	//else echo "<td><font color=red> Đang thi </font>";
	//$private=intval($row->private);
	//if ($private==0) echo "<td><font color=blue>Mở</font>";
	//else echo "<td><font color=red>Kín</font>";
	echo "</tr>";
	$color=!$color;
}
echo "</table></center>";
mysql_free_result($result);
?>

<?php }?>

			</div>
			<div class="col-b4 qcao">
					<?php include("qcao.php");?>
			</div>
			<div class="clear"></div>
		</div>
	</div>
</div>
<?php include_once("include/footer.php");?>
